import { Pipe, PipeTransform } from '@angular/core';
/**
 * 自定义的管道
 */

/**
 * 进行货币格式转换
 * 
 * currency的格式为：1 || '1'
 * <span>{{currency | money }}</span>
 */
@Pipe({ name: 'money' })
export class MoneyFormatPipe implements PipeTransform {
  transform(value: any, type: string = '￥'): any {
    switch (type) {
      case '￥':
        return MoneyFormatPipe.moneyFormat(value);
      case '$':
        return MoneyFormatPipe.moneyFormat(value, '$');
    }
    return '';
  }

  /**
   * 货币格式转换
   * @param value 数值
   * @param symbol 符号
   * @param thousand 
   * @param decimal 
   */
  private static moneyFormat(value, places?, symbol?, thousand?, decimal?): any {
    places = !isNaN(places = Math.abs(places)) ? places : 2;
    // symbol = symbol !== undefined ? symbol : '￥';
    thousand = thousand || ",";
    decimal = decimal || ".";
    let number = value
    let negative = number < 0 ? "-" : ""
    let i: any = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "";
    let j = i.length;
    j = j > 3 ? j % 3 : 0;
    //symbol + 
    return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
  };
}

//进行状态格式转换
@Pipe({ name: 'status' })
export class StatusFormatPipe implements PipeTransform {
  transform(value: any, type: string): any {

    switch (type) {
      case 'status_id':
        switch (value) {
          case '0':
            return '待处理';
          case '1':
            return '处理中';
          case '2':
            return '已完结';
        }
        break;
    }
    return '';
  }
}

/**
 * 对图片字符串格式转换
 * 
 * imgs的格式为:"1,2,3"
 * 
 * <img *ngFor='let link of imgs|LinkText'/>
 */
@Pipe({ name: 'LinkText' })
export class LinkTextFormatPipe implements PipeTransform {
  transform(value: any): string[] {
    let _type = Object.prototype.toString.call(value);
    if (_type === "[object Array]") {
      value = value.join(',');
    }
    value = value || 'assets/imgs/defaultImage.jpg';
    return value.split(',');
  }
}

/**
 * 转换成json对象
 * 
 * str的格式为:"{'name':'Jack'}"
 * 
 * <span>{{ str | jsonParse}}</span>
 */
// @Pipe({ name: 'jsonParse' })
// export class JsonParsePipe implements PipeTransform {
//   transform(value: string): any {
//     value = value || '[]';
//     let data = JSON.parse(value);
//     return data[0] ? data : null;
//   }
// }

/**
 * 转换成tel:18571137555
 *
 * str的格式为:"18571137555"
 *
 * <span>{{ str | telParse}}</span>
 */
@Pipe({ name: 'telParse' })
export class TelParsePipe implements PipeTransform {
  transform(value: string): any {
    value = value || '';
    return value != "" ? "tel:" + value : null;
  }
}

@Pipe({
  name: 'week',
})
export class MyWeekPipe implements PipeTransform {
  transform(date:Date) {
    let week="";
    if(date.getDay()==1){
      week="周一"
    }
    if(date.getDay()==2){
      week="周二"
    }
    if(date.getDay()==3){
      week="周三"
    }
    if(date.getDay()==4){
      week="周四"
    }
    if(date.getDay()==5){
      week="周五"
    }
    if(date.getDay()==6){
      week="周六"
    }
    if(date.getDay()==0){
      week="周日"
    }
    if(date.getDate()==new Date().getDate()){
      week="今天"
    }

    return (date.getMonth()+1)+"月"+date.getDate()+"日"+"["+week+"]";
  }
}
  
@Pipe({
  name: 'kcisornot',
})
export class KucunPipe implements PipeTransform {
  transform(date:number) {
      if(date>0){
        return "有库存"
      }else{
        return "无库存"
      }
  }
}